﻿using mozhi.smarterp.BasicData.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace mozhi.smarterp.BasicData
{
    public class Tag : AggregateRootBase
    {
        public TagType Type { get; private set; }

        public List<TagItem> Items { get; private set; }

        public Tag(Guid id, string code, string name, TagType type) : base(id, code, name)
        {
            Type = type;

            if (Type == default)
                throw new UserFriendlyException("标签类型不能为空", "100000");

            Items = new List<TagItem>();
        }

        public void ChangeItems(List<TagItem> tagItems)
        {
            if (tagItems == null || tagItems.Count == 0)
            {
                Items.Clear();
                return;
            }

            var duplicateCodes = tagItems.GroupBy(x => x.Code).Where(x => x.ToList().Count > 1).Select(x => x.Key).ToList();

            if (duplicateCodes.Count > 0)
                throw new UserFriendlyException($"项目存在重复的编码：{string.Join(",", duplicateCodes)}", "100000");

            foreach (var item in tagItems)
            {
                var match = Items.FirstOrDefault(x => x.Code == item.Code);

                if (match == null)
                    Items.Add(item);
                else
                    match.Change(item.Code, item.Name);
            }

            var codes = tagItems.Select(x => x.Code).ToList();

            //然后移除掉原有的数据
            var deleted = Items.Where(x => !codes.Contains(x.Code)).ToList();

            if (deleted.Count > 0)
                deleted.ForEach(x => Items.Remove(x));
        }

        public void ChangeType(TagType type)
        {
            if (type == default)
                throw new UserFriendlyException("标签类型不能为空", "100000");

            if (type == TagType.List && Items == null || Items.Count == 0)
                throw new UserFriendlyException("标签类型为列表时，项目不能为空", "100000");

            if (type != TagType.List)
                Items = new List<TagItem>();

            Type = type;
        }
    }

    public class TagItem : AuditedEntity<Guid> , IMultiTenant
    {
        public  bool Activated { protected set; get; }
        public  Guid? TenantId { get; protected set; }

        public string Code { private set; get; }

        public string Name { private set; get; }

        public Guid? TagId { private set; get; }

        public TagItem(Guid id, string code, string name)
        {
            Id = id;
            Code = code;
            Name = name;

            Validate();
        }

        public void Change(string code, string name)
        {
            Code = code;
            Name = name;
            Validate();
        }

        private void Validate()
        {
            if (Code.IsNullOrEmpty())
                throw new UserFriendlyException("标签项编码不能为空");

            if (Name.IsNullOrEmpty())
                throw new UserFriendlyException("标签项名称不能为空");
        }
    }
}
